const fs = require('fs')
const join = require('path').join;
const { turndown } = require('markdown-read');
const { exit } = require('process');

const argv = process.argv
if (argv.length <= 2) {
    console.log('usage: node html2md.js srcPath dstPath')
    exit
}
let srcPath = argv[2] !== undefined ? argv[2] : "html"
let dstPath = argv[3] !== undefined ? argv[3] : "md"
console.log(srcPath, dstPath);

// let srcPath = "html";
// let dstPath = "md";
try{
  fs.mkdirSync(dstPath);
}catch (error){
}

let files = fs.readdirSync(srcPath);
files.forEach(function (item, index) {
    let fPath = join(srcPath,item);
    console.log(fPath, item, index);

    let stat = fs.statSync(fPath);
    if (stat.isFile() === true &&  -1 !== item.indexOf(".html")) { 
      let md = toMd(fPath);
      let dstFile = join(dstPath, item).replace(".html", ".md");
      fs.writeFileSync(dstFile, md, 'utf8' );
    }
});

function toMd(file){
  let content = fs.readFileSync(file, 'utf8' );
  // read markdown from html
  let md = turndown(content);

  // 去除代码行
  // md = md.replace(/\d{1,2}\s{2,10}/g, "");
  return md;
}









